//
// Created by vista on 2017/12/9.
//

#ifndef P_P8_H
#define P_P8_H

#include <vector>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;

class Solution {
public:
    int myAtoi(string str) {

        while (*str.begin() == ' '){
            str.erase(str.begin());
        }
        if(str.length() == 0)
            return 0;

        bool minus = false;

        if(*str.begin() == '+')
            str.erase(str.begin());
        else if(*str.begin() == '-') {
            str.erase(str.begin());
            minus = true;
        }

        if( *str.begin() > '9' || *str.begin() < '0'){
            return 0;
        }

        long val = 0;

        for (int j = 0; j < str.length(); ++j) {

            if( str[j] <= '9' && str[j] >= '0'){
                val = val * 10 + str[j] - '0';
                if(val > INT32_MAX){
                    if(minus){
                        return INT32_MIN;
                    }else
                        return INT32_MAX;
                }
            } else {
                break;
            }
        }

        return minus?(int)-val:(int)val;

    }
};

#endif //P_P8_H
